package com.zxl.cityservice.controller;

import com.google.gson.Gson;
import com.zxl.cityservice.entity.Employee;
import com.zxl.cityservice.service.LoginService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bearworker on 17-4-15.
 */

@Controller
@RequestMapping("/login")
public class LoginController {
    private Logger logger = Logger.getLogger(LoginController.class);

    Gson gson = new Gson();

    @Autowired
    private LoginService loginService;

    @RequestMapping("/valid")
    public ModelAndView valid(String username, String password){
        ModelAndView mv = new ModelAndView();
        logger.debug("username:"+username);
        logger.debug("password:"+password);
        Map map = new HashMap();
        map.put("username",username);
        map.put("password",password);
        Employee employee = loginService.getOneRes(map);
        if(employee!= null && employee.getId()>0) {
            mv.addObject("username", username);
            mv.setViewName("main");
        }
        return mv;
    }

    @RequestMapping("/applogin")
    @ResponseBody
    public String applogin(HttpServletResponse response, HttpServletRequest request){
        logger.debug(request.getParameter("username"));
        logger.debug(request.getParameter("password"));

        Map<String,String> map = new HashMap<String,String>();

        map.put("code","0");
        map.put("msg","ok");

        logger.debug(gson.toJson(map));

        return gson.toJson(map);
    }

    @RequestMapping("/head")
    public ModelAndView head(String username){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("layout/head");
        mv.addObject("username",username);
        return mv;
    }

    @RequestMapping("/alluser")
    @ResponseBody
    public String alluser(){
        List<Employee> list = loginService.getAll(0,1000000);
        return gson.toJson(list);
    }

    @RequestMapping("report")
    public ModelAndView report(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("employee/employee");
        return mv;
    }
}
